/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.location;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;

import com.aroundj2me.controller.ScreenController;

public class LocationView extends Form implements CommandListener, Runnable {
	private LocationService service;
	private Command currentLocation;

	public LocationView(LocationService service) {
		super("Location Provider");
		this.service = service;
		createCommands();
		this.setCommandListener(this);
		determineLocation();
	}

	private void createCommands() {
		currentLocation = new Command("my location", "my location",
				Command.SCREEN, 0);
		this.addCommand(currentLocation);
	}

	public void showLocation(Location location) {
		this.deleteAll();
		StringItem lat = new StringItem("Latitude: ", String.valueOf(location
				.getQualifiedCoordinates().getLatitude()));
		StringItem lon = new StringItem("Longitude: ", String.valueOf(location
				.getQualifiedCoordinates().getLongitude()));

		this.append(lat);
		this.append(lon);
	}

	public void showMessage(String msg) {
		this.deleteAll();
		if (msg == null)
			this.append(new StringItem("Info:", "determining location..."));
		else
			this.append(new StringItem("Error:", msg));
	}

	public void commandAction(Command c, Displayable d) {
		if (c.getCommandType() == Command.BACK) {
			ScreenController.getInstance().last();
		} else if (c == currentLocation) {
			determineLocation();
		}
	}
	
	public void determineLocation(){
		showMessage(null);
		Thread t = new Thread(this);
		t.start();
	}

	public void run() {
		try {
			Location currentPosition = service.getCurrentLocation();
			showLocation(currentPosition);
		} catch (LocationException e) {
			showMessage(e.getMessage());
		} catch (InterruptedException e) {
			showMessage(e.getMessage());
		}
	}
}
